package com.richard.base.view.gdxView.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.fragment.app.FragmentManager
import com.richard.base.view.gdxView.adapter.GdxAdapter
import com.ks.component.ipimage.fragment.GdxFragment
import com.richard.base.R

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/9/21     3:57 PM
 * 用途:
 ***************************************
 */
class GdxView @JvmOverloads constructor(
        context: Context,
        private val fragmentManager: FragmentManager,
        private val atlasPath: String,
        private val jsonPath: String,

        attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var gdxFragment: GdxFragment? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.base_widget_gdx_view, this)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (gdxFragment == null) {
            gdxFragment =  GdxFragment.newGdxFragment(atlasPath, jsonPath)
            this.fragmentManager.beginTransaction().replace(R.id.fl_gdx_root, gdxFragment!!).commitAllowingStateLoss()
        }
    }


    /**
     * 执行动作
     * @param action String 执行的动作名称
     * @param loop Boolean 是否循环执行该动作
     */
    fun startAnimation(action: String, loop: Boolean, listener: GdxAdapter.ActionCompleteListener) {
        gdxFragment?.doAction(action, loop, listener)
    }

    /**
     * 播放文字转语音
     * @param text String
     */
    fun playTextAudio(text: String) {

    }

    /**
     * 播放本地语音
     * @param filePath String
     */
    fun playLocalAudio(filePath: String) {

    }


}